/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.tomcatspace;

import com.zwu.util.CreateDeepNode;
import java.io.File;
import javax.jcr.*;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;
import org.apache.jackrabbit.value.StringValue;

/**
 *
 * @author Administrator
 */
public class FileWithNotFileContent {



    public static void main(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("data/tomcat"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));


        try {

            CreateDeepNode.createDeepPath(session.getRootNode(), "path1/path2/path3");
            session.save();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.logout();
        }

    }

    public static void testQuery(Workspace wk) throws Exception {
        QueryManager qm = wk.getQueryManager();
        //String query = "select excerpt(.) from nt:resource where contains(., 'us')";
        String query = "select * from [nt:resource] as file where contains(file.*, 'pdf')  ";

        Query q = qm.createQuery(query, Query.JCR_SQL2);
        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();

            Node nn = r.getNode();
            System.out.println(nn.getParent().getPath());
            PropertyIterator pi2 = nn.getProperties();
            while (pi2.hasNext()) {
                System.out.println(pi2.nextProperty().getName());
            }
            // nn.getParent().remove();

            //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
            //System.out.print(excerpt.getString());
        }


        //Thread.currentThread().sleep(10000);
    }

    public static void main2(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("data/tomcat"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));


        try {

            Node root = session.getRootNode();
            Node tryNode = root.addNode("TryFolder", "nt:folder");
            Node fileNode = tryNode.addNode("myfile", "nt:file");

            //fileNode.addNode("jcr:content", "nt:resource");
            Node tagNode = fileNode.addNode("jcr:content", "nt:unstructured");
            Value[] values = new Value[]{new StringValue("car"), new StringValue("tweddle")};
            tagNode.setProperty("tag", values);
            session.save();
            //Node contentNode = fileNode.addNode("my content", "jcr:content");




        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.logout();
        }

    }


}
